package com.prs.crm.action.oa;

import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.oa.Vehicle;
import com.prs.crm.domain.oa.VehicleRepairAndMaintenance;
import com.prs.crm.service.oa.VehicleRepairAndMaintenanceService;
import com.prs.crm.service.oa.VehicleService;

public class VehicleRepairAndMaintenanceAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	private VehicleRepairAndMaintenance vehicleRepairAndMaintenance;
	private VehicleRepairAndMaintenanceService vehicleRepairAndMaintenanceService;
	private List<Vehicle> vehicles;
	private VehicleService vehicleService;
	private Integer[] vehicleRepairAndMaintenanceIds;
	
	@Action("listVehicleRepairAndMaintenance")
	public String listVehicleRepairAndMaintenance() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				VehicleRepairAndMaintenance.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.vehicleRepairAndMaintenanceService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listVehicleMaintenance")
	public String listVehicleMaintenance() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				VehicleRepairAndMaintenance.class);
		criteria.add(Restrictions.eq("state", "保养"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.vehicleRepairAndMaintenanceService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("editVehicleRepairAndMaintenance")
	public String editVehicleRepairAndMaintenance() {
		this.vehicles=this.vehicleService.getVehicleAll();
		return SUCCESS;
	}@Action("viewVehicleRepairAndMaintenance")
	public String viewVehicleRepairAndMaintenance() {
		return SUCCESS;
	}
	@Action(value = "saveVehicleRepairAndMaintenance", results = {
			@Result(name = "success", type = "redirect", location = "listVehicleRepairAndMaintenance"),
			@Result(name = "error", type = "chain", location = "editVehicleRepairAndMaintenance") })
	public String saveVehicleRepairAndMaintenance(){
		if(vehicleRepairAndMaintenance.getState()==null){
			this.addActionError("请选择类别！");
			return ERROR;
		}
		if("维修".equals(vehicleRepairAndMaintenance.getState())){
			if(vehicleRepairAndMaintenance.getRepairTime()==null){
				this.addActionError("维修日期不能为空！");
				return ERROR;
			}else{
				vehicleRepairAndMaintenance.setState("维修");
			}
		}else{
			if(vehicleRepairAndMaintenance.getNextTime()==null){
				this.addActionError("下次保养时间不能为空！");
				return ERROR;
			}else{
				vehicleRepairAndMaintenance.setState("保养");
			}
		}
		this.vehicleRepairAndMaintenanceService.save(vehicleRepairAndMaintenance);
		return SUCCESS;
	}
	@Action(value = "removeVehicleRepairAndMaintenance", results = {
			@Result(name = "success", type = "redirect", location = "listVehicleRepairAndMaintenance"),
			@Result(name = "error", type = "chain", location = "listVehicleRepairAndMaintenance") })
	public String removeVehicleRepairAndMaintenance(){
		if(vehicleRepairAndMaintenanceIds==null){
			this.addActionError("请选择操作行！");
			return ERROR;
		}
		this.getVehicleRepairAndMaintenanceService().batchRemove(vehicleRepairAndMaintenanceIds);
		return SUCCESS;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public VehicleRepairAndMaintenance getVehicleRepairAndMaintenance() {
		return vehicleRepairAndMaintenance;
	}
	public void setVehicleRepairAndMaintenance(
			VehicleRepairAndMaintenance vehicleRepairAndMaintenance) {
		this.vehicleRepairAndMaintenance = vehicleRepairAndMaintenance;
	}
	public VehicleRepairAndMaintenanceService getVehicleRepairAndMaintenanceService() {
		return vehicleRepairAndMaintenanceService;
	}
	public void setVehicleRepairAndMaintenanceService(
			VehicleRepairAndMaintenanceService vehicleRepairAndMaintenanceService) {
		this.vehicleRepairAndMaintenanceService = vehicleRepairAndMaintenanceService;
	}
	public List<Vehicle> getVehicles() {
		return vehicles;
	}
	public void setVehicles(List<Vehicle> vehicles) {
		this.vehicles = vehicles;
	}
	public VehicleService getVehicleService() {
		return vehicleService;
	}
	public void setVehicleService(VehicleService vehicleService) {
		this.vehicleService = vehicleService;
	}
	public Integer[] getVehicleRepairAndMaintenanceIds() {
		return vehicleRepairAndMaintenanceIds;
	}
	public void setVehicleRepairAndMaintenanceIds(
			Integer[] vehicleRepairAndMaintenanceIds) {
		this.vehicleRepairAndMaintenanceIds = vehicleRepairAndMaintenanceIds;
	}
	
}
